package com.example.service;

import com.example.entity.LikeUser;
import com.example.entity.Post;
import com.example.mapper.LikeUserMapper;
import com.example.mapper.PostMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class LikeUserService
{

    @Resource
    private LikeUserMapper likeUserMapper;
    @Resource
    private PostMapper postMapper;

    public void add(LikeUser likeUser){
        likeUserMapper.insert(likeUser);
    }

    public void deleteById(LikeUser likeUser) {
        likeUserMapper.deleteById(likeUser);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids){
//            this.deleteById(id);
        }
    }

    public void updateById(LikeUser likeUser) {
        likeUserMapper.updateById(likeUser);
    }

    public LikeUser selectById(Integer id) {
        return likeUserMapper.selectById(id);
    }

    public List<LikeUser> selectAll(LikeUser likeUser) {
        return likeUserMapper.selectAll(likeUser);
    }

    public PageInfo<LikeUser> selectPage(LikeUser likeUser, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<LikeUser> userList = likeUserMapper.selectAll(likeUser);
        return PageInfo.of(userList);
    }
    public PageInfo<Post> selectPostPage(LikeUser likeUser, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<LikeUser> theLikeUserList=selectAll(likeUser);
        List<Post> postList=new ArrayList<>();
        Post thePost=new Post();
        for(LikeUser item:theLikeUserList)
        {
            thePost.setUid(item.getLikeId());
            postList.addAll(postMapper.selectAll(thePost));
        }
        return PageInfo.of(postList);
    }
}
